<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Authors	: Tung, Nguyen Dam                                        |
// | Email		: mask_final@yahoo.com									  | 								
// | Mobile		: (+84) 936 885 466                                       |
// | Date		: 06/2011												  |							
// +----------------------------------------------------------------------+
// | Copyrights (C) 2011                                                  |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+

if(!function_exists('paging')){  	
	function paging($currentPage, $link_per_page, $pageSum, $arritem = array()){	
		// define item
		if(count($arritem) <= 0){			
			$arritem = array(	
				'first'		=>	'{First}', 
				'prev'		=>	'{Prev}', 
				'next'		=>	'{Next}', 
				'last'		=>	'{Last}', 
				'item'		=> 	'{Item}'
			);
		}	
		
		// Get current page
		$currentPage = ($currentPage <= 1) ? 1 : $currentPage;
		
		if($pageSum > 0 && ($currentPage <= $pageSum) ){
			$requestURI = $_SERVER['REQUEST_URI'];
			$pattern 	= '/\?paging=[0-9]+/';
			$requestURI = preg_replace($pattern, '/', $requestURI);
			$pattern 	= '/\&paging=[0-9]+/';
			$requestURI = preg_replace($pattern, '/', $requestURI);
			
			$pattern = '/({(.*)})/';
			preg_match_all($pattern, $arritem['first'], $first);
			preg_match_all($pattern, $arritem['prev'], $prev);
			preg_match_all($pattern, $arritem['next'], $next);
			preg_match_all($pattern, $arritem['last'], $last);
			preg_match_all($pattern, $arritem['item'], $item);
				
			$arrRequest = explode('?', $requestURI );
			if(count($arrRequest) < 2){
				$param = "?paging=";
			}else{
				$param = "&paging=";
			}
			
			if($currentPage > 1){
				$pageNum = $currentPage - 1;			
				echo str_replace($first[1][0], '<span class="pFirst"><a href="' . $requestURI . $param . '1">'. $first[2][0] . '</a></span>', $arritem['first']);			
				echo str_replace($prev[1][0], '<span class="pPrev"><a href="' . $requestURI . $param . $pageNum . '">'. $prev[2][0] . '</a></span>', $arritem['prev']);
			}else{
				echo str_replace($first[1][0], '<span class="pFirst">'. $first[2][0] . '</span>', $arritem['first']);			
				echo str_replace($prev[1][0], '<span class="pPrev">'. $prev[2][0] . '</span>', $arritem['prev']);
			}
					
			
			if($link_per_page >= $pageSum){
				for($i=1; $i<=$pageSum;$i++){								
					if($i == $currentPage){				
						echo str_replace($item[1][0], '<span class="pItem"><a class="pActive" href="' . $requestURI . $param . $i.'">'. $i .'</a></span>', $arritem['item']);
					}
					else{
						echo str_replace($item[1][0], '<span class="pItem"><a href="' . $requestURI . $param . $i.'">'. $i . '</a></span>', $arritem['item']);
					}
				}
			}else{
				if($currentPage > $link_per_page){
					$point_start = ($link_per_page * floor(($currentPage / $link_per_page))) + 1;
					$point_end = $link_per_page * (floor($currentPage / $link_per_page) +1);
					if($point_end <= $pageSum){
						for($i=$point_start; $i<=$point_end;$i++){		
							echo str_replace($item[1][0], '<span class="pItem"><a href="' . $requestURI . $param . $i . '">'. $i .'</a></span>', $arritem['item']);	
						}
					}else{
						for($i=$point_start; $i<=$pageSum;$i++){							
							echo str_replace($item[1][0], '<span class="pItem"><a href="' . $requestURI . $param . $i . '">'. $i .'</a></span>', $arritem['item']);
						}
					}
				}else{
					for($i=1; $i<=$link_per_page;$i++){			
						echo str_replace($item[1][0], '<span class="pItem"><a href="' . $requestURI . $param . $i . '">'. $i .'</a></span>', $arritem['item']);		
					}
				}
			}	
			
			if($currentPage < $pageSum){
				$pageNum = $currentPage + 1;		
				echo str_replace($next[1][0], '<span class="pNext"><a href="' . $requestURI . $param . $pageNum . '">'. $next[2][0] . '</a></span>', $arritem['next']);				
				echo str_replace($last[1][0], '<span class="pLast"><a href="' . $requestURI . $param . $pageSum . '">'. $last[2][0] . '</a></span>', $arritem['last']);					
			}else{
				echo str_replace($next[1][0], '<span class="pNext">'. $next[2][0] . '</span>', $arritem['next']);				
				echo str_replace($last[1][0], '<span class="pLast">'. $last[2][0] . '</span>', $arritem['last']);	
			}	
		}			
	}	
}


